package written.w20241224flygyy.Q1;

import java.util.concurrent.*;

/**
 * Q1
 *
 * @Author : Ruoyu Wang
 * @DateTime : 2024/12/24 19:45
 */

public class Main {
    static volatile int x;
    static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        int total = 100;
        int batch = 10;

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
                10,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        for (int epoch = 0; epoch < total / batch; epoch++) {
            System.out.println("epoch:" + epoch);
            x=0;
            for (int i = 0; i < batch; i++) {
                int id = epoch * batch + i;
                threadPoolExecutor.execute(() -> {
                    System.out.print("TaskID:" + id + " ");
                    synchronized (lock) {
                        x++;
                    }
                });
            }
            while(x<10){

            }
            System.out.println();
        }
        threadPoolExecutor.shutdown();
    }

    //    public static void main(String[] args) throws InterruptedException {
//        int total = 100;
//        int batch = 10;
//
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
//                10,
//                60,
//                TimeUnit.SECONDS,
//                new LinkedBlockingQueue<>());
//        for (int epoch = 0; epoch < total / batch; epoch++) {
//            System.out.println("epoch:" + epoch);
//            CountDownLatch countDownLatch = new CountDownLatch(batch);
//            for (int i = 0; i < batch; i++) {
//                int id = epoch * batch + i;
//                threadPoolExecutor.execute(() -> {
//                    System.out.print("TaskID:" + id + " ");
//                    countDownLatch.countDown();
//                });
//            }
//            countDownLatch.await();
//            System.out.println();
//        }
//        threadPoolExecutor.shutdown();
//    }
}
